//
//  IDOLineChart1ViewController.m
//  IDOCharts
//
//  Created by 卓勇 on 2017/8/16.
//  Copyright © 2017年 zhuoyong. All rights reserved.
//

#import "IDOLineChart1ViewController.h"
#import "DateValueFormatter.h"
#import "SetValueFormatter.h"
#import "SymbolsValueFormatter.h"

/**
 参考地址：
    http://www.jianshu.com/p/cf16fccd7b8e
    http://www.jianshu.com/p/27c756b536df
 */

@interface IDOLineChart1ViewController () <ChartViewDelegate>

@property (nonatomic, strong) LineChartView *lineChartView;

@end

@implementation IDOLineChart1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view addSubview:self.lineChartView];
    
    // 设置X轴
    [self setChartXAxis];
    
    // 设置Y轴
    [self setChartYAxis];
    
    // 气泡
    BalloonMarker *marker = [[BalloonMarker alloc]
                             initWithColor:[UIColor clearColor]
                             font:[UIFont systemFontOfSize:12.0]
                             textColor:UIColor.blueColor
                             insets:UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0)];
    marker.chartView = self.lineChartView;
    self.lineChartView.marker = marker;
    
    // 加载数据
    [self loadData];
    
}

#pragma mark - ChartViewDelegate
- (void)chartValueSelected:(ChartViewBase * _Nonnull)chartView entry:(ChartDataEntry * _Nonnull)entry highlight:(ChartHighlight * _Nonnull)highlight {

}

- (void)chartValueNothingSelected:(ChartViewBase * _Nonnull)chartView {

}

- (void)chartScaled:(ChartViewBase * _Nonnull)chartView scaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY {

}

- (void)chartTranslated:(ChartViewBase * _Nonnull)chartView dX:(CGFloat)dX dY:(CGFloat)dY {

}

#pragma mark - load data
- (void)loadData {
    // 数据
    
    // X轴数据
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    [xVals addObject:@"08-09"];
    [xVals addObject:@"08-10"];
    [xVals addObject:@"08-11"];
    [xVals addObject:@"08-12"];
    [xVals addObject:@"08-13"];
    [xVals addObject:@"08-14"];
    [xVals addObject:@"08-15"];
    self.lineChartView.xAxis.valueFormatter = [[DateValueFormatter alloc] initWithArr:xVals];

    // Y轴数据
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:0 y:3.91]];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:1 y:3.95]];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:2 y:3.99]];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:3 y:3.91]];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:4 y:3.96]];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:5 y:3.98]];
    [yVals addObject:[[ChartDataEntry alloc] initWithX:6 y:3.91]];
    
    LineChartDataSet *dataSet = [[LineChartDataSet alloc] initWithValues:yVals];
    
    self.lineChartView.leftAxis.valueFormatter = [[SymbolsValueFormatter alloc] init];
    // dataSet.valueFormatter = [[SetValueFormatter alloc] initWithArr:yVals];
    
    dataSet.lineWidth = 1; // 折线宽度
    [dataSet setColor:[UIColor blueColor]]; // 折线颜色
    
    dataSet.drawCirclesEnabled = YES; // 是否绘制拐点
    dataSet.circleRadius = 4.0f; // 拐点半径
    dataSet.circleColors = @[[UIColor blueColor]]; // 拐点颜色
    
    dataSet.drawCircleHoleEnabled = NO; // 是否绘制中间的空心
    dataSet.circleHoleRadius = 4.0f; // 空心的半径
    dataSet.circleHoleColor = [UIColor whiteColor]; // 空心的颜色
    
    // dataSet.mode = LineChartModeCubicBezier; // 模式为曲线模式
    // dataSet.cubicIntensity = 0.1;// 曲线弧度
    
    dataSet.drawValuesEnabled = NO; // 文字显示 (YES：显示/NO：不显示)
    dataSet.highlightEnabled = YES; // 选中拐点,是否开启高亮效果(显示十字线)
    dataSet.highlightColor = [UIColor clearColor]; // 十字线颜色
    
    // 填充色
    NSArray *gradientColors = @[
                                (id)[ChartColorTemplates colorFromString:@"#FFFFFFFF"].CGColor,
                                (id)[ChartColorTemplates colorFromString:@"#FF007FFF"].CGColor
                                ];
    CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    dataSet.fillAlpha = 0.6f;
    dataSet.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
    dataSet.drawFilledEnabled = YES;
    CGGradientRelease(gradient);
    
    // 最终数据
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:dataSet];
    
    LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
    self.lineChartView.data = data;
    
    // 高亮最后一个点
    [self.lineChartView highlightValueWithX:6 y:3.91 dataSetIndex:0 callDelegate:NO];
    
    // 移动到指定点
    // [self.lineChartView moveViewToX:self.weights.count];
    
    // 出现动画
    [self.lineChartView animateWithXAxisDuration:0.5];
}


- (void)setChartYAxis {
    // Y轴设置
    // self.lineChartView.rightAxis.enabled = NO; // 不绘制右边轴
    ChartYAxis *rightAxis = self.lineChartView.rightAxis;//获取左边Y轴
    rightAxis.axisMinValue = 3.91 - 0.5; // 设置Y轴的最小值
    rightAxis.axisMaxValue = 3.99 + 0.2; // 设置Y轴的最大值
    rightAxis.axisLineColor = [UIColor clearColor]; // Y轴颜色
    rightAxis.gridAntialiasEnabled = YES; // 开启抗锯齿
    rightAxis.labelTextColor = [UIColor clearColor]; // 文字颜色
    // rightAxis.labelCount = 5;// Y轴label数量，数值不一定，如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
    // rightAxis.forceLabelsEnabled = YES; // 不强制绘制指定数量的label
    
    ChartYAxis *leftAxis = self.lineChartView.leftAxis;//获取左边Y轴
    leftAxis.axisMinValue = 3.91 - 0.5; // 设置Y轴的最小值
    leftAxis.axisMaxValue = 3.99 + 0.2; // 设置Y轴的最大值
    leftAxis.axisLineColor = [UIColor clearColor]; // Y轴颜色
    leftAxis.gridAntialiasEnabled = YES; // 开启抗锯齿
    // leftAxis.labelPosition = YAxisLabelPositionOutsideChart; // label位置
    // leftAxis.labelTextColor = [UIColor blackColor]; // 文字颜色
    // leftAxis.labelFont = [UIFont systemFontOfSize:10.0f]; // 文字字体
    // leftAxis.gridColor = [UIColor grayColor]; // 网格线颜色
    // leftAxis.inverted = NO; // 是否将Y轴进行上下翻转
    // leftAxis.labelCount = 5;// Y轴label数量，数值不一定，如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
    // leftAxis.forceLabelsEnabled = YES; // 不强制绘制指定数量的label
}

- (void)setChartXAxis {
    ChartXAxis *xAxis = self.lineChartView.xAxis;
    xAxis.granularityEnabled = YES; // 设置重复的值不显示
    xAxis.labelPosition= XAxisLabelPositionBottom; // 设置x轴数据在底部
    xAxis.gridColor = [UIColor clearColor];
    xAxis.labelTextColor = [UIColor blackColor]; // 文字颜色
    xAxis.axisLineColor = [UIColor grayColor];
    xAxis.axisLineWidth = 1.0 / [UIScreen mainScreen].scale; // 设置X轴线宽
    xAxis.drawGridLinesEnabled = NO; // 不绘制网格线
}


#pragma mark - getter
- (LineChartView *)lineChartView {
    if (!_lineChartView) {
        
        _lineChartView = [[LineChartView alloc] initWithFrame:CGRectMake(15, 64 + 15, self.view.frame.size.width - 30, 200)];
        _lineChartView.backgroundColor = [UIColor whiteColor];
        _lineChartView.delegate = self;
        _lineChartView.noDataText = @"暂无数据";
        _lineChartView.descriptionText = @"";

//        _lineChartView.highlightPerTapEnabled = NO; // 点击高亮
//        _lineChartView.highlightPerDragEnabled = NO; // 拖动高亮
        _lineChartView.legend.enabled = NO; // 不显示图例说明
        _lineChartView.scaleXEnabled = NO; // 取消X轴缩放
        _lineChartView.scaleYEnabled = NO;// 取消Y轴缩放
        _lineChartView.scaleEnabled = NO; // 缩放
        _lineChartView.doubleTapToZoomEnabled = NO;// 取消双击缩放
        _lineChartView.dragEnabled = YES; // 启用拖拽图标
        _lineChartView.dragDecelerationEnabled = NO; // 拖拽后是否有惯性效果
        _lineChartView.dragDecelerationFrictionCoef = 0; // 拖拽后惯性效果的摩擦系数(0~1)，数值越小，惯性越不明显
    }
    
    return _lineChartView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
